package web.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

public class ResultDisposeAction extends ActionSupport {

    //获取表单的数据,得到用户想要的模块信息
    private String module;
    //获取session域对象
    private HttpSession session = ServletActionContext.getRequest().getSession();

    public String getMyResult(){

        ArrayList<String> divList = new ArrayList<>();
        System.out.println("表单传入的值为:"+module);

        if (module.contains("table")){
            //从session中拿到tableList对象
            ArrayList<String> tableList = (ArrayList<String>) session.getAttribute("table");
//            System.out.println("tableList的内容为:"+tableList);
            divList.add(format(tableList));
        }

        if (module.contains("paragraph")){
            //从session中拿到tableList对象
            ArrayList<String> paragraphList = (ArrayList<String>) session.getAttribute("paragraph");
            divList.add(format(paragraphList));
        }

        System.out.println("格式化好的divList内容为:"+divList);
        session.setAttribute("divList",divList);

        return SUCCESS;
    }

    private String format(ArrayList<String> tableList) {
        StringBuffer buffer = new StringBuffer();
        for (String table : tableList) {
            buffer.append(table);
        }
        return buffer.toString();
    }


    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }
}
